package projectxxx;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;

public class MainStockGui extends JPanel implements ActionListener
{
	private JPanel baseP=null, northBtnP=null, baseP2=null, searchAreaP=null,
				   changeP=null, baseP3=null, northHeadingP=null;
	private JButton searchStockB=null, addStockB=null, searchB=null;
	private JLabel searchLbl=null, stockAttribLbl=null;
	private JTextField searchTxt=null;
	private Vector searchV=null;
	JComboBox searchJcb=null;
	MainWestStockPanel mainWest=null;
	MainWestStockPanel mw=null;
	MainEastStockPanel mainEast=null;
	MainSouthStockPanel mainSouth=null;
	OtherSouthStockPanel otherSouth=null;
	String myDriver = "com.mysql.jdbc.Driver";
	String ipOfDB = "localhost";
	MySQLdriverConnection msc=null;
	Connection conn=null;
	
	public MainStockGui() 
	{
		mw = new MainWestStockPanel();
		baseP = new JPanel(new BorderLayout());
		
		northBtnP = new JPanel(new GridLayout(0,2));
		searchStockB = new JButton("Search-4-Stock Item");
		searchStockB.setToolTipText("Find Stock Item");
		searchStockB.addActionListener(this);
		addStockB = new JButton("Add Stock");
		addStockB.setToolTipText("Add New Stock Item");
		addStockB.addActionListener(this);
		northBtnP.add(searchStockB);
		northBtnP.add(addStockB);
		
		baseP2 = new JPanel(new BorderLayout());
		
		baseP.add(northBtnP, BorderLayout.NORTH);
		baseP.add(baseP2, BorderLayout.CENTER);
		
		searchAreaP = new JPanel();
		BoxLayout b1 = new BoxLayout(searchAreaP, BoxLayout.X_AXIS);
		searchAreaP.setLayout(b1);
		searchLbl = new JLabel(" Enter Stock Item Title/Barcode/Stock ID ");
		searchTxt = new JTextField();
		searchV = new Vector();
		searchV.addElement("Title");
		searchV.addElement("Barcode");
		searchV.addElement("Stock ID");
		searchJcb = new JComboBox(searchV);
		searchB = new JButton(" Search ");
		searchB.addActionListener(this);
		searchAreaP.add(searchLbl);
		searchAreaP.add(searchTxt);
		searchAreaP.add(searchJcb);
		searchAreaP.add(searchB);
		searchAreaP.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		baseP3 = new JPanel(new BorderLayout());
		
		baseP2.add(searchAreaP, BorderLayout.NORTH);
		baseP2.add(baseP3, BorderLayout.CENTER);
		
		northHeadingP = new JPanel();
		stockAttribLbl = new JLabel(" Media Stock Attributes ");
		northHeadingP.add(stockAttribLbl);
		
		changeP = new JPanel(new BorderLayout());
		
		baseP3.add(northHeadingP, BorderLayout.NORTH);
		baseP3.add(changeP, BorderLayout.CENTER);
		
		//setup all panels
		mainWest = new MainWestStockPanel();
		mainEast = new MainEastStockPanel();
		mainSouth = new MainSouthStockPanel();
		otherSouth = new OtherSouthStockPanel();
		
		changeP.add(mainWest, BorderLayout.WEST);
		changeP.add(mainEast, BorderLayout.EAST);
		changeP.add(mainSouth, BorderLayout.SOUTH);
	
		//setup "this" Jpanel
		this.setLayout(new GridLayout(1,1));
		this.add(baseP);
	}//end of MainStockGui constructor
	
	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if(source == searchStockB)
		{
			try
			{
				changeP.remove(otherSouth);
				changeP.add(mainSouth, BorderLayout.SOUTH);
				this.validate();
				this.repaint();
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}	
		}
		if(source == addStockB)
		{
			try
			{
				changeP.remove(mainSouth);
				changeP.add(otherSouth, BorderLayout.SOUTH);
				this.validate();
				this.repaint();
			}
			catch(Exception excep)
			{
				excep.printStackTrace();
			}
		}
		if(source == searchB)
		{
			System.out.println(" In searchB...");
			String criteria = (String)searchJcb.getSelectedItem();
			System.out.println(criteria);
			
			String searchText = searchTxt.getText();
			System.out.println(searchText);
			
			StockManager sm = new StockManager();
			Stock s = sm.findStock(searchText, criteria);
			
			mw.medStockIdTxt.setText(s.getStockId());
			mw.mediaTypeJcb.setSelectedItem(s.getStockTypeId());  
			mw.titleTxt.setText(s.getTitle());
			mw.statusJcb.setSelectedItem(s.getStatus());
			mw.barcodeTxt.setText(s.getBarcode());
			mw.brandTxt.setText(s.getBarcode());
			mw.serialNoTxt.setText(s.getSerialNo());
			mw.descriptionTxt.setText(s.getDescription());
			mw.catNameJcb.setSelectedItem(s.getCatId());           
			mw.runTimeTxt.setText(s.getRunTime());
			mw.releaseDateJdc.setEnabled(false);
			mw.genreJcb.setSelectedItem(s.getGenre());
			mw.ageRestTxt.setText(s.getAgeRest());
			mw.directorTxt.setText(s.getDirector());
			mw.actor1Txt.setText(s.getActor1());
			mw.actor2Txt.setText(s.getActor2());
			mw.lastRentedJdc.setEnabled(false);
			this.repaint();
			this.validate();
		}
		
	}		
}//end of MainStockGui class


   